package info.uno;

import java.util.ArrayList;
import java.util.Collections;

public class Board 
{
	ArrayList<ArrayList> players = new ArrayList<ArrayList>();
	
	ArrayList<Card> deckHand = new ArrayList<Card>();
	
	Card currentCard;
	String currentPlayer;
	
	boolean gameIsComplete = false;
	
	ArrayList<String> playersList = new ArrayList<String>();
	
	public Board(ArrayList<String> playersList)
	{
		
		if(playersList.size() > 0 && playersList.size() < 14)
		{
			initDeck();
			shuffleDeck();

			this.playersList = playersList;
		
			for(int i = 0; i < playersList.size(); i++)
			{
				players.add(new ArrayList<Card>());
			}
		
			for(ArrayList a : players)
			{
			for(int p = 0; p < 7; p++)
				{
					a.add(deckHand.remove(p));
				}
			}
			
			this.currentCard = this.deckHand.get(0);
			this.currentPlayer = this.playersList.get(0);

		}
		else
		{
			System.out.println("Invaid Player Count.");
		}
	}
	
	public String getNextPlayer()
	{
		for(int i = 0; i <= this.playersList.size(); i++)
		{
			if(this.currentPlayer.equals(playersList.get(i)))
			{
				if(i >= this.playersList.size()-1)
				{
					return this.playersList.get(0);
				}
				else
				{
					return this.playersList.get(i+1);
				}
			}
		}
		return "";
	}
	
	public ArrayList<ArrayList> getPlayersCards()
	{
		return this.players;
	}
	
	public void start(UnoSocket u)
	{
		while(!this.gameIsComplete)
		{
			
		}
	}
	
	public String getCurrentPlayer()
	{
		return this.currentPlayer;
	}
	
	public Card getCurrentCard()
	{
		return this.currentCard;
	}
	
	public void addCardToDeck(Card cc)
	{
		this.deckHand.add(cc);
	}
	
	public void removeCardFromPlayer()
	{
		
	}
	
	public void setCurrentCard(Card cc)
	{
		this.currentCard = cc;
	}
	
	public String[] getCards()
	{
		String temp[] = new String[playersList.size()];
		String temp2 = "";
		
		int i = 0;
		
		for(ArrayList a : players)
		{
			for(Object b : a)
			{
				temp2 = temp2 + ((Card) b).getLabel() + " ";
			}
			
			temp[i] = temp2;
			temp2 = "";
			
			i++;
		}
		
		return temp;
	}
	
	public void sortDeckHand()
	{
		
	}
	
	public void printCards()
	{
		int i = 0;
		
		for(ArrayList a : players)
		{
			System.out.println(playersList.get(i)+":");
			
			for(Object b : a)
			{
				System.out.print(((Card) b).getLabel() + " ");
			}
			
			System.out.println();
			
			i++;
		}
		
		System.out.println("Deck Hand: ");
		
		for(Card c : this.deckHand)
		{
			System.out.print(c.getLabel() + " ");
		}
	}
	
	public void shuffleDeck()
	{
		Collections.shuffle(deckHand);
	}
	
	private void initDeck()
	{
		deckHand.add(new Card(Card.B0));
		deckHand.add(new Card(Card.B1));
		deckHand.add(new Card(Card.B2));
		deckHand.add(new Card(Card.B3));
		deckHand.add(new Card(Card.B4));
		deckHand.add(new Card(Card.B5));
		deckHand.add(new Card(Card.B6));
		deckHand.add(new Card(Card.B7));
		deckHand.add(new Card(Card.B8));
		deckHand.add(new Card(Card.B9));
		deckHand.add(new Card(Card.B1));
		deckHand.add(new Card(Card.B2));
		deckHand.add(new Card(Card.B3));
		deckHand.add(new Card(Card.B4));
		deckHand.add(new Card(Card.B5));
		deckHand.add(new Card(Card.B6));
		deckHand.add(new Card(Card.B7));
		deckHand.add(new Card(Card.B8));
		deckHand.add(new Card(Card.B9));
		deckHand.add(new Card(Card.BDT));
		deckHand.add(new Card(Card.BDT));
		deckHand.add(new Card(Card.BS));
		deckHand.add(new Card(Card.BS));
		deckHand.add(new Card(Card.BR));
		deckHand.add(new Card(Card.BR));
		
		deckHand.add(new Card(Card.Y0));
		deckHand.add(new Card(Card.Y1));
		deckHand.add(new Card(Card.Y2));
		deckHand.add(new Card(Card.Y3));
		deckHand.add(new Card(Card.Y4));
		deckHand.add(new Card(Card.Y5));
		deckHand.add(new Card(Card.Y6));
		deckHand.add(new Card(Card.Y7));
		deckHand.add(new Card(Card.Y8));
		deckHand.add(new Card(Card.Y9));
		deckHand.add(new Card(Card.Y1));
		deckHand.add(new Card(Card.Y2));
		deckHand.add(new Card(Card.Y3));
		deckHand.add(new Card(Card.Y4));
		deckHand.add(new Card(Card.Y5));
		deckHand.add(new Card(Card.Y6));
		deckHand.add(new Card(Card.Y7));
		deckHand.add(new Card(Card.Y8));
		deckHand.add(new Card(Card.Y9));
		deckHand.add(new Card(Card.YDT));
		deckHand.add(new Card(Card.YDT));
		deckHand.add(new Card(Card.YS));
		deckHand.add(new Card(Card.YS));
		deckHand.add(new Card(Card.YR));
		deckHand.add(new Card(Card.YR));

		deckHand.add(new Card(Card.G0));
		deckHand.add(new Card(Card.G1));
		deckHand.add(new Card(Card.G2));
		deckHand.add(new Card(Card.G3));
		deckHand.add(new Card(Card.G4));
		deckHand.add(new Card(Card.G5));
		deckHand.add(new Card(Card.G6));
		deckHand.add(new Card(Card.G7));
		deckHand.add(new Card(Card.G8));
		deckHand.add(new Card(Card.G9));
		deckHand.add(new Card(Card.G1));
		deckHand.add(new Card(Card.G2));
		deckHand.add(new Card(Card.G3));
		deckHand.add(new Card(Card.G4));
		deckHand.add(new Card(Card.G5));
		deckHand.add(new Card(Card.G6));
		deckHand.add(new Card(Card.G7));
		deckHand.add(new Card(Card.G8));
		deckHand.add(new Card(Card.G9));
		deckHand.add(new Card(Card.GDT));
		deckHand.add(new Card(Card.GDT));
		deckHand.add(new Card(Card.GS));
		deckHand.add(new Card(Card.GS));
		deckHand.add(new Card(Card.GR));
		deckHand.add(new Card(Card.GR));
		
		deckHand.add(new Card(Card.R0));
		deckHand.add(new Card(Card.R1));
		deckHand.add(new Card(Card.R2));
		deckHand.add(new Card(Card.R3));
		deckHand.add(new Card(Card.R4));
		deckHand.add(new Card(Card.R5));
		deckHand.add(new Card(Card.R6));
		deckHand.add(new Card(Card.R7));
		deckHand.add(new Card(Card.R8));
		deckHand.add(new Card(Card.R9));
		deckHand.add(new Card(Card.R1));
		deckHand.add(new Card(Card.R2));
		deckHand.add(new Card(Card.R3));
		deckHand.add(new Card(Card.R4));
		deckHand.add(new Card(Card.R5));
		deckHand.add(new Card(Card.R6));
		deckHand.add(new Card(Card.R7));
		deckHand.add(new Card(Card.R8));
		deckHand.add(new Card(Card.R9));
		deckHand.add(new Card(Card.RDT));
		deckHand.add(new Card(Card.RDT));
		deckHand.add(new Card(Card.RS));
		deckHand.add(new Card(Card.RS));
		deckHand.add(new Card(Card.RR));
		deckHand.add(new Card(Card.RR));
		
		deckHand.add(new Card(Card.W));
		deckHand.add(new Card(Card.W));
		deckHand.add(new Card(Card.W));
		deckHand.add(new Card(Card.W));
		
		deckHand.add(new Card(Card.WDF));
		deckHand.add(new Card(Card.WDF));
		deckHand.add(new Card(Card.WDF));
		deckHand.add(new Card(Card.WDF));
	}
}